<template>
	<uni-popup ref='pwdPopUp' type="center" :maskClick='false'>
		<view class="content" @tap="hiddenKeyboard">
			<view class="title">支付密码</view>
			<view class="flex-jc-ac">
				<view class="input-row" @tap.stop="showKeyboard">
					<view :class="['item', { 'item-active': valueLength == index }]" v-for="(k, index) in 6"
						:key="index">{{ value.charAt(index)? "●":""  }}</view>
				</view>
			</view>
			<view class="forget font-size-base color-tip"
				@click="$util.redirectTo('/otherpages/member/info_edit/info_edit',{action:'paypassword'})">
				忘记支付密码</view>
			<view class="btn flex">
				<view class="btn-close color-join-cart" @click="hide">
					取消
				</view>
				<view class="btn-ok color-base-bg" @click="handleSendData">
					确认
				</view>
			</view>
		</view>
		<DigitalKeyboard @change="contToggle" @closeKeyboard="closeKeyboard" :keyBoardSwitch="keyBoardSwitch">
		</DigitalKeyboard>
	</uni-popup>

</template>

<script>
	import DigitalKeyboard from '@/components/passwordp-opup/digital-keyboard/digital-keyboard.vue';
	export default {
		components: {
			DigitalKeyboard
		},
		data() {
			return {
				keyBoardSwitch: false,
				length: 6,
				value: '',
				valueLength: null
			}

		},
		methods: {
			async show() {
				let res = await this.$api.sendRequest({
					url: '/api/register/config',
					async: false
				});
				let registerConfig = res.data.value;
				if (registerConfig.is_paypwd == 1) { //需要校验支付密码
					this.value = ''
					this.valueLength = null
					this.$refs.pwdPopUp.open()
				} else {
					this.$emit('complete')
				}
			},
			hide() {
				this.$refs.pwdPopUp.close()
			},
			showKeyboard() {
				this.valueLength = this.value.length == 6 ? 5 : this.value.length;
				this.keyBoardSwitch = true;
			},
			hiddenKeyboard(bool, key) {
				this.keyBoardSwitch = false;
				this.valueLength = null
			},
			// 发送数据
			handleSendData() {
				if (!this.value || this.value.length != 6) {
					this.$util.showToast({
						title: '支付密码输入格式有误'
					});
					return;
				}
				//校验支付密码
				this.$api.sendRequest({
					url: '/api/member/checkpaypassword',
					data: {
						pay_password: this.value
					},
					success: res => {
						if (res.code == 0) {
							this.$refs.pwdPopUp.close();
							this.$emit('complete');
						} else {
							this.$util.showToast({
								title: res.message
							});
						}
					}
				})
			},
			// 更新
			contToggle(k) {
				let len = this.value.length;
				if (k === '') {
					this.value = this.value.substring(0, len - 1);
					this.valueLength = this.value.length;
					return;
				}
				if (this.length >= len + 1) {
					this.value += k.toString();
					this.valueLength = len + 1;
					if (this.valueLength >= this.length) {
						this.keyBoardSwitch = false;
					}
				} else {
					this.keyBoardSwitch = false;
				}
			},
			// 结束
			closeKeyboard(value) {
				this.$emit('pwdData', value)
			},
		}
	}
</script>

<style scoped lang="scss">
	/deep/.uni-popup__wrapper {
		background: transparent !important;
	}

	/deep/.uni-popup__wrapper-box {
		height: auto !important;
	}

	.flex-jc-ac {
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.flex {
		display: flex;
	}

	.content {
		width: 80vw;
		min-height: 354rpx;
		background-color: #fff;
		border-radius: 16rpx;
		box-shadow: 1px 1px 10px 0 #666;
		box-sizing: border-box;
		padding: 30rpx;
		color: #000;
		overflow: hidden;

		.title {
			font-size: 36rpx;
			margin-bottom: 30rpx;
			text-align: center;
			border-bottom: 1px solid #f2f2f2;
			padding: 10rpx 0 20rpx;
		}

		.btn {
			display: flex;
			align-items: center;
			// justify-content: flex-end;
			justify-content: space-between;
			margin: 20rpx 70rpx 10rpx 70rpx;
		}

		.btn-close {
			// border: 1px solid #4689f2;
			height: 60rpx;
			line-height: 60rpx;
			box-sizing: border-box;
			margin-right: 20rpx;
			color: #fff;
			border-radius: 10rpx;
			padding: 0 14rpx;
			text-align: center;
			min-width: 160rpx;
		}

		.btn-ok {
			min-width: 160rpx;
			color: #fff;
			line-height: 60rpx;
			box-sizing: border-box;
			text-align: center;
			border-radius: 10rpx;
			padding: 0 14rpx;
			height: 60rpx;
			margin-right: 20rpx;
			background-color: #4689f2;
		}

		.forget {
			padding: 10rpx 30rpx 0rpx 30rpx;
			text-align: right;
			text-decoration: underline;
		}
	}

	.input-row {
		height: 80rpx;
		display: flex;
		align-items: center;
		box-sizing: border-box;
		font-size: 20rpx;
		text-align: center;

		.item {
			float: left;
			width: 40upx;
			height: 60upx;
			line-height: 60upx;
			margin: 0 10upx;
			border-bottom: 1px solid #c8c8c8;
			background: #fff;

			box-sizing: border-box;
		}

		.item-active {
			position: relative;
			transform: scale(1.2);
		}
	}
</style>